<?php
class IndexAction extends Action {
    public function index(){
    	$this->display();
    }
 	public function login(){
    	header("Content-Type:text/html; charset=utf-8");
    	$Dao= M("User");
    	if($Dao->create()){
    		$condition['username'] = $Dao->username;
    		$condition['password'] = MD5($Dao->password);   	
		// 查询数据
			$list = $Dao->where ( $condition )->find ();
			if ($list) {
				//echo '账号正确';
				session('uid',$list['uid']);
				session('nickname',$list['nickname']);
				$this->display("home");
			} else {
				$this->error("账号/密码错误");
				
    		}
    	}
    }
    public function logout(){
    	
    	session(null); 
    	$this->display("index");
    }
 	public function home(){
 		
 		if(session('uid'))
    		$this->display();
    	else
    		$this->error("尚未登录");
    
    }
    public function regedit(){
    	header("Content-Type:text/html; charset=utf-8");

    $Dao = D("User");

    if($Dao->create()){
        if($lastInsId = $Dao->add()){
            $this->success("注册成功！");
        } else {
            $this->error("数据库插入错误");
        }
    }else{
    	$this->error($Dao->getError());
       //exit($Dao->getError().' [ <a href="javascript:history.back()">返 回</a> ]');
    }
    }
    
    
	public function read(){

 	$Dao = M("User");
    
    // 查询数据
    $list = $Dao->select();
    //dump($list);	// 用 dump() 可以在调试阶段查看数据是否已读取

    // 模板变量赋值
    $this->assign("list", $list);
    
    // 输出模板
    $this->display();
     
		
	}

public function checkName(){
	$Dao = M("User");
	$condition['username'] = $_POST['username'];
    // 查询数据
    $list = $Dao->where($condition)->select();
    if ($list){
        $this->success('用户名已存在，请更改！');
    }else{
        $this->error('用户名可以注册');
    }
}

	public function test(){

	$file=D("file");
	//在这里应该从session中读取当前的project名称，然后从数据库中读取
	//$project=$_SESSION['project'];	
	//为了测试方便，这里只用project2
	$project="project2";
	$condition['project'] = $project;
	$list=$file->where($condition)->select();
	$this->assign('list',$list);
	$this->display();
	
	}
	
	public function tpl1()
	{
	session('project','殷三三');	
	$file=D("file");
	//在这里应该从session中读取当前的project名称，然后从数据库中读取
	//$project=$_SESSION['project'];	
	//为了测试方便，这里只用project2
	$project="project2";
	$condition['project'] = $project;
	$list=$file->where($condition)->select();
	$this->assign('list',$list);
	$this->assign('project',$_SESSION['project']);
		$this->display();
	}
}